package com.wf.demo.job.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.wf.demo.info.pojo.Device;
import com.wf.demo.info.service.DeviceService;
import com.wf.demo.info.util.LocalDateTimeUtils;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.job.pojo.DataDevice;
import com.wf.demo.job.service.DataDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.sl.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: wf
 * @Date: 2019/11/30 20:21
 * @Description:
 */
@Slf4j
@RestController
public class DataDeviceController {
    @Autowired
    DataDeviceService dataDeviceService;
    @Autowired
    DeviceService deviceService;
    //单个设备的所有数据
    @GetMapping("/devices/{did}/dataDevices")
    public Page4Navigator<DataDevice> list(@PathVariable("did")long did, @RequestParam(value = "start",defaultValue = "0")int start,
                                           @RequestParam(value = "size",defaultValue = "5")int size) throws Exception{
        start = start<0? 0:start;
        Page4Navigator<DataDevice> page = dataDeviceService.list(did,start,size,5);
        return page;
    }
    @PostMapping("/exportExcel")
    public void export(HttpServletResponse response,@RequestBody Map params) throws IOException {

        long id= Long.parseLong(params.get("id").toString());
        Device device=deviceService.get(id);
        LocalDateTime startTime= LocalDateTimeUtils.getLocalDateTimeByString(LocalDateTimeUtils.formatter_yyyyMMddHHmmss,params.get("startTime").toString());
        LocalDateTime endTime= LocalDateTimeUtils.getLocalDateTimeByString(LocalDateTimeUtils.formatter_yyyyMMddHHmmss,params.get("endTime").toString());
        List<DataDevice> list = dataDeviceService.getDeviceDataByTime(id,startTime,endTime);  //搜索得到excel
            try {
                // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
                response.setContentType("application/vnd.ms-excel");
                response.setCharacterEncoding("utf-8");
                // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
                String fileName = URLEncoder.encode(device.getCsNumber()+"Data" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()),
                        "UTF-8").replaceAll("\\+", "%20");  //excel文件名为 1-11-10-09-9999data+日期
                response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
                EasyExcel.write(response.getOutputStream(), DataDevice.class).sheet("模板").doWrite(list);
            }catch (Exception e){
               log.error("下载Excel异常：{}", e.getMessage());
                throw new RuntimeException("下载Excel异常");
            }

    }


}
